﻿using System;
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.TeamFoundation.VersionControl.Client;

namespace TfsPolicyPack.CustomRegexPathPolicy
{
    public class MatchingPendingCheckinPendingChanges : IPendingCheckinPendingChanges
    {
        #region Non Public Members

        private readonly IPendingCheckinPendingChanges _originalPendingChanges;
        private readonly PolicySettings _policySettings;

        #endregion

        #region Events

        public event AffectedTeamProjectsEventHandler AffectedTeamProjectsChanged
        {
            add { _originalPendingChanges.AffectedTeamProjectsChanged += value; }
            remove { _originalPendingChanges.AffectedTeamProjectsChanged -= value; }
        }

        public event EventHandler CheckedPendingChangesChanged
        {
            add { _originalPendingChanges.CheckedPendingChangesChanged += value; }
            remove { _originalPendingChanges.CheckedPendingChangesChanged -= value; }
        }

        #endregion

        #region Properties

        public string[] AffectedTeamProjectPaths
        {
            get { return _originalPendingChanges.AffectedTeamProjectPaths; }
        }

        public PendingChange[] AllPendingChanges
        {
            get { return _originalPendingChanges.AllPendingChanges; }
        }

        public string Comment
        {
            get { return _originalPendingChanges.Comment; }
            set { _originalPendingChanges.Comment = value; }
        }

        public Workspace Workspace
        {
            get { return _originalPendingChanges.Workspace; }
        }

        public PendingChange[] CheckedPendingChanges
        {
            get { return _originalPendingChanges.CheckedPendingChanges.Where(IsPendingChangeToConsider).ToArray(); }
            set { _originalPendingChanges.CheckedPendingChanges = value; }
        }

        #endregion

        #region Constructor

        public MatchingPendingCheckinPendingChanges(IPendingCheckinPendingChanges originalPendingChanges, PolicySettings policySettings)
        {
            _originalPendingChanges = originalPendingChanges;
            _policySettings = policySettings;
        }

        #endregion

        #region Non Public Methods

        private bool IsPendingChangeToConsider(PendingChange pendingChange)
        {
            bool isToConsider = (_policySettings.IncludeList.Count == 0);

            foreach (string includeRegexPattern in _policySettings.IncludeList)
                isToConsider |= Regex.IsMatch(pendingChange.ServerItem, includeRegexPattern, RegexOptions.Singleline);

            if (!isToConsider)
                return false;

            foreach (string excludeRegexPattern in _policySettings.ExcludeList)
            {
                if (Regex.IsMatch(pendingChange.ServerItem, excludeRegexPattern, RegexOptions.Singleline))
                    return false;
            }
            
            return true;
        }

        #endregion
    }
}